package com.lanlion.portlet.http;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.io.IOUtils;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.cookie.Cookie;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;

import java.io.InputStream;
import java.util.List;

/**
 * Author: zhangyu1@qiku.com
 * Title: http请求工具类
 * Remark: 支持Get，post请求
 * Time:2017/1/3
 */
public class HttpSSLHelper {

    //http连接池管理
    HttpConnectionManager httpConnectionManager ;

    public HttpSSLHelper(String cert,String key){
        httpConnectionManager = new HttpConnectionManager(cert,key);
    }

    /**
     * doget请求
     *
     * @param path 请求地址
     * @return HttpResult 请求处理结果
     * @throws Exception
     */
    public   HttpResult doGet(String path) throws Exception {
        CloseableHttpClient httpClient = httpConnectionManager.getHttpClient();
        HttpGet httpget = new HttpGet(path);
        HttpResult httpRet = new HttpResult();
        CloseableHttpResponse response = null;
        response = httpClient.execute(httpget,httpConnectionManager.getContext());
        InputStream in = response.getEntity().getContent();
        List<Cookie> cookies = httpConnectionManager.getContext().getCookieStore().getCookies();
        httpRet.setCookies(cookies); //返回Cookie
        httpRet.setResult(IOUtils.toString(in,"UTF-8")); //返回结果
        httpRet.setStatusCode(response.getStatusLine().getStatusCode());//返回状态码
        in.close();
        if (response != null) {
            response.close();
        }
        return httpRet;
    }

    /**
     * 发送json post请求
     * @param path 请求地址
     * @param params 请求json内容
     * @return HttpResult 请求处理结果
     * @throws Exception
     */
    public   HttpResult doPost4JSON(String path, JSONObject params) throws Exception {
        StringEntity stringEntity = new StringEntity(params.toString(), "UTF-8");//解决中文乱码问题
        stringEntity.setContentEncoding("UTF-8");
        stringEntity.setContentType("application/json");
        return doPost(path,stringEntity);
    }

    public   HttpResult doPost4JSON(String path, JSONObject params,String encoding) throws Exception {
        StringEntity stringEntity = new StringEntity(params.toString(), encoding);//解决中文乱码问题
        stringEntity.setContentEncoding(encoding);
        stringEntity.setContentType("application/json");
        return doPost(path,stringEntity,encoding);
    }

    /**
     * 发送xml post请求
     * @param path 请求地址
     * @param params xml内容
     * @return HttpResult 请求处理结果
     * @throws Exception
     */
    public   HttpResult doPost4XML(String path, String params) throws Exception {
        StringEntity stringEntity = new StringEntity(params.toString(), "UTF-8");//解决中文乱码问题
        stringEntity.setContentEncoding("UTF-8");
        stringEntity.setContentType("application/xml");
        return doPost(path,stringEntity);
    }


    /**
     * post请求
     * @param path 请求地址
     * @param stringEntity 请求内容
     * @return
     * @throws Exception
     */
    public   HttpResult doPost(String path, StringEntity stringEntity) throws Exception {
        return doPost(path,stringEntity,"UTF-8");
    }

    public   HttpResult doPost(String path, StringEntity stringEntity, String encoding) throws Exception {
        CloseableHttpClient httpClient = httpConnectionManager.getHttpClient();
        HttpPost httpPost = new HttpPost(path);
        HttpResult httpRet = new HttpResult();
        CloseableHttpResponse response = null;
        httpPost.setEntity(stringEntity);
        response = httpClient.execute(httpPost,httpConnectionManager.getContext());
        InputStream in = response.getEntity().getContent();
        httpRet.setResult(IOUtils.toString(in,encoding));
        httpRet.setStatusCode(response.getStatusLine().getStatusCode());
        httpRet.setCookies(httpConnectionManager.getContext().getCookieStore().getCookies());
        in.close();
        if (response != null) {
            response.close();
        }
        return httpRet;
    }



}
